//
//  CYGFBTool.m
//  CreditAddressBook
//
//  Created by LE on 16/1/8.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYGFBTool.h"
#import "ParamModel.h"
#import "YSMD5.h"
#import "CYPayViewController.h"
#import "CYMeHttpTool.h"
#import "CYCompanyUserTool.h"
#import "CYLoginHttpTool.h"
#import "CYJumpsTool.h"
#import "CYCompanyUserModel.h"
#import "CYJsonInfo.h"
#import "MJExtension.h"
#import <GopayNewPlatformLibraryWeb/GopayNewPlatformLibraryWeb.h>
#import "CYUserRechargeM.h"
#import "CYCompanyRechargeM.h"
#import "CYGFBParam.h"
#import "MBProgressHUD+MJ.h"

@interface CYGFBTool ()<GopayNewPlatformDelegate>
@property (nonatomic, copy)NSString *tfTranAmt;//充值金额

@property(nonatomic, assign) CYUserType userType;
@property (nonatomic, strong)GopayNewPlatform *gopayNewPlatform;

@end

@implementation CYGFBTool
+ (void)rechargeAmount:(NSString *)amount userType:(CYUserType)userType {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];//设置状态栏为黑色
    CYGFBTool *tool = [self sharedWithUserType:userType];
    tool.tfTranAmt = amount;
    [tool rechargeButton];
}

+ (instancetype)sharedWithUserType:(CYUserType)userType {
    CYGFBTool *tool = [[CYGFBTool alloc] init];
    if (tool) {
        [tool initData];
        tool.userType = userType;
    }
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        
//    });
    return tool;
}

#pragma mark -
- (void)initData {
    self.gopayNewPlatform = [[GopayNewPlatform alloc] init];
    self.gopayNewPlatform.delegate = self;
}
/**
 *  参数设置
 */
- (CYGFBParam *)setupParamModel {
    CYGFBParam *param = [CYGFBParam new];
    param.charset = @"1";
    param.language = @"1";
    param.signType = @"1";
    param.merchantID = @"0000011536";//商户ID
    param.backgroundMerUrl = @"http://www.credit-manage.com:8058/doPayResponse.htm";
    param.virCardNoIn = @"0000000002000004268";
    param.isRepeatSubmit = @"1";
    param.goodsName = @"天下无赖充值";
    param.goodsDetail = @"天下无赖充值";
    param.buyerContact = [GopayNewPlatform getGopayIdentifierForPayment];
    return param;
}
/**
 *  充值核心代码
 */
- (void)rechargeButton {
    [CYCompanyUserTool getUserWithUseType:self.userType user:^(CYUser *user) {
        NSDictionary *param = nil;
        if (self.tfTranAmt && !self.tfTranAmt.isEmpty) {
            param = @{@"userId":user.userId,@"token":user.token, @"account":self.tfTranAmt};
        }
        if (param) {
            [MBProgressHUD showMessage:nil];
            [self getUserRechargeOrderWithParam:param success:^(CYUserRechargeM *userRecharge) {
                [MBProgressHUD hideHUD];
                CYGFBParam *GFBparam = [self setupParamModel];
                GFBparam.merOrderNum = userRecharge.rechargeId;
                GFBparam.tranAmt = userRecharge.account;
                GFBparam.tranDateTime = userRecharge.createTimeFormatter;
                NSError *error = nil;
                NSDictionary *param = [GFBparam keyValuesWithError:&error];
                if (!error) {
                    [self.gopayNewPlatform startGopayWithParameters:param];
                } else {
                    
                    [MBProgressHUD showError:@"充值失败"];
                }
            } failure:^(NSString *message, BOOL isLogin) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:message];
                if (!isLogin) {
                    
                }
            }];
        } else {
            [MBProgressHUD showError:@"充值金额有误"];
        }
    } companyUser:^(CYCompanyUserModel *companyUser) {
        
    }];
}

#pragma mark - 网络请求
- (void)add {
    if (self.userType == CYUserTypePerson) {}
    if (self.userType == CYUserTypeCompany) {
        //判断如果是公司的user
        CYCompanyUserModel *companyuser = [CYCompanyUserTool companyUser];
        NSString *carcompanyUserid = companyuser.carCompanyUserId;
        NSString *companytoken = companyuser.token;
        NSLog(@"carcompanyUserid = %@,companytoken = %@",carcompanyUserid,companytoken);
        [CYHttpTool postWithURL:[CYURL shared].getCompanyRechargeOrder params:@{@"userId":carcompanyUserid,@"token":companytoken,@"account":@"100.00"} success:^(id json) {
            NSLog(@"jsoncom = %@",json);
            
        } failure:^(NSError *error) {
            
        }];
    }
}
/**
 *  生成用户订单号
 */
- (void)getUserRechargeOrderWithParam:(NSDictionary *)param success:(void(^)(CYUserRechargeM *userRecharge))success failure:(HttpRequestFailure2)failure {
    [CYHttpTool postWithURL:[CYURL shared].getUserRechargeOrder params:param success:^(id json) {
        CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
        if (jsonInfo.isSuccess) {
            CYUserRechargeM *model = [CYUserRechargeM objectWithKeyValues:jsonInfo.data];
            if (success) {
                success(model);
            }
        } else if (jsonInfo.isNoLogin) {
            if (failure) {
                failure(kCYLoginStatusError, NO);
            }
        }
    } failure:^(NSError *error) {
        CYLog(@"生成用户订单号错误");
        if (failure) {
            failure(@"服务器连接错误", YES);
        }
    }];
}
/**
 *  生成企业订单号
 */
- (void)getCompanyRechargeOrderWithParam:(NSDictionary *)param success:(void(^)(CYCompanyRechargeM *userRecharge))success failure:(HttpRequestFailure2)failure {
    [CYHttpTool postWithURL:[CYURL shared].getCompanyRechargeOrder params:param success:^(id json) {
        CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
        if (jsonInfo.isSuccess) {
            CYCompanyRechargeM *model = [CYCompanyRechargeM objectWithKeyValues:jsonInfo.data];
            if (success) {
                success(model);
            }
        } else if (jsonInfo.isNoLogin) {
            if (failure) {
                failure(kCYLoginStatusError, NO);
            }
        }
    } failure:^(NSError *error) {
        CYLog(@"生成企业订单号错误");
        if (failure) {
            failure(@"服务器连接错误", YES);
        }
    }];
}

#pragma mark - GopayNewPlatformDelegate
- (void)gopayDidClosed {
    CYLog(@"已经关闭交易");
}
@end
